/************************************************************************
            WingedEdges2Polygons.cpp - Copyright Claudio Botta

This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Boston, MA 02110, USA

This file was generated on Sat May 17 2008 at 16:41:23
The original location of this file is geomagick/WingedEdges2Polygons.cpp
**************************************************************************/

#include "WingedEdges2Polygons.h"
using namespace geomagick;

/**
* \class WingedEdges2Polygons
* \brief Offering a Conversion from WingedEdges to Polygons
*/
Polygons* WingedEdges2Polygons::process(WingedEdges* inWingedEdges)
{
        Polygons* output = new Polygons();
        //vector<WEVertex*>* WEVertexCoordinates = (inWingedEdges->getVertices());
        int PolygonIndex = 0;
	#if DEBUG
	cout << "Test: " << endl;
	cout << (inWingedEdges->getFaces()) << endl;
	#endif
	
        foreach(int incidentEdge, *(inWingedEdges->getFaces()) ) {
		
                WEdge* firstWEdge = inWingedEdges->getEdgeAt(incidentEdge);
		#if DEBUG
		cout << "Polygonindex " << PolygonIndex << endl;
		cout << "left: " << firstWEdge->LeftFace << endl;
		cout << "right: " << firstWEdge->RightFace << endl;
		#endif
		WEdge* currentWEdge;
		//WEdge* nextWEdge;
		
		Polygon* currentPolygon = NULL;
		vector<int> visited_edges;
		visited_edges.clear();
		visited_edges.push_back(incidentEdge);
                // Is our Polygon the left or the right Polygon?
              
                        // Polygon is the left Face
			currentPolygon = new Polygon();
				// curentPolygon.push_back();
			int indexCurrentEdge;
			  if (firstWEdge->LeftFace == PolygonIndex) {
				currentPolygon->Vertices.push_back(firstWEdge->StartVertex);
				currentPolygon->Vertices.push_back(firstWEdge->EndVertex);
			 	indexCurrentEdge = firstWEdge->Left_succ;
				 currentWEdge = inWingedEdges->getEdgeAt(firstWEdge->Left_succ);
				}

			   else {
					if(firstWEdge->RightFace == PolygonIndex){
						currentPolygon->Vertices.push_back(firstWEdge->StartVertex);
				currentPolygon->Vertices.push_back(firstWEdge->EndVertex);
						indexCurrentEdge = firstWEdge->Right_succ;
						 currentWEdge = inWingedEdges->getEdgeAt(firstWEdge->Right_succ);
					}
				}
                       
			//visited_edges.push_back(firstWEdge->Left_succ);
                        while (firstWEdge!=currentWEdge && currentWEdge != NULL && 1/*notIn(indexCurrentEdge,visited_edges)*/) {
				#if DEBUG
                                cout << "succ_Left: " << currentWEdge->Left_succ;
				cout << "succ_Right:" << currentWEdge->Right_succ << endl;
				#endif	

				// DECIDE Left OR RIGHT
				if(currentWEdge->LeftFace == PolygonIndex){
				currentPolygon->Vertices.push_back(currentWEdge->StartVertex);
				currentPolygon->Vertices.push_back(currentWEdge->EndVertex);
			
				currentWEdge = inWingedEdges->getEdgeAt(currentWEdge->Left_succ);
				visited_edges.push_back(indexCurrentEdge);
				indexCurrentEdge = currentWEdge->Left_succ;
				}else{
				if(currentWEdge->RightFace == PolygonIndex){
					
				currentPolygon->Vertices.push_back(currentWEdge->EndVertex);
				currentPolygon->Vertices.push_back(currentWEdge->StartVertex);
			
				currentWEdge = inWingedEdges->getEdgeAt(currentWEdge->Right_succ);
				visited_edges.push_back(indexCurrentEdge);
				indexCurrentEdge = currentWEdge->Right_succ;
				}else{
				
					break;
				}
				}
				//currentWEdge = inWingedEdges->getEdgeAt(indexCurrentEdge);
				// add the Edge to the newly created Polygon
                        }
                
		if(currentPolygon != NULL)
		output->addPolygon(*currentPolygon);

                PolygonIndex++;
        } // foreach

       foreach(WEVertex* Vertex, *(inWingedEdges->getVertices()))
	{
		output->addVertexCoordinates(Vertex->Coordinates);
	}

	return output;
}
